package com.moddevapp.phoneutil;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.os.Bundle;
import android.os.SystemClock;
import android.provider.CallLog.Calls;
import android.util.Log;


public class PhoneUtilActivity extends Activity {
	private static int lastCount = 0;
	private static final int PERIOD=30000;  // 5 minutes
	private static final String TAG = "PhoneUtil";
	
	//get missed call count
	public int getMissedCallCount(Context context) {
        Cursor cursor = getApplicationContext().getContentResolver().query(
	            Calls.CONTENT_URI, 
	            null, 
	            Calls.TYPE +  " = ? AND " + Calls.NEW + " = ?", 
	            new String[] { Integer.toString(Calls.MISSED_TYPE), "1" }, 
	            Calls.DATE + " DESC ");

	    //this is the number of missed calls
	    //for your case you may need to track this number
	    //so that you can figure out when it changes
	    return cursor.getCount(); 		
	}
	
	public int checkCurrentMissedCall() {
        Cursor cursor = getApplicationContext().getContentResolver().query(
	            Calls.CONTENT_URI, 
	            null, 
	            Calls.TYPE +  " = ? AND " + Calls.NEW + " = ?", 
	            new String[] { Integer.toString(Calls.MISSED_TYPE), "1" }, 
	            Calls.DATE + " DESC ");
        
        int saveCurCount = cursor.getCount();
        Log.d(TAG, "Current Missed " + saveCurCount + " last missed " + lastCount);
        //show all missed count between last missed and current missed
        if(saveCurCount > lastCount) {
        	if(cursor.moveToLast()) {
	        	Log.d(TAG, "Number: " + cursor.getString(6));
	        	Log.d(TAG, "Data: " + cursor.getString(8));
        	}
        	
            AlarmManager mgr=(AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
            Intent i=new Intent(getApplicationContext(), OnAlarmReceiver.class);
            i.putExtra("Number", cursor.getString(6));
            PendingIntent pi=PendingIntent.getBroadcast(getApplicationContext(), 0,
                                                      i, 0);
            
            mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                              SystemClock.elapsedRealtime()+60000,
                              PERIOD,
                              pi);
        }
        else if(saveCurCount < lastCount) {
        	//user clean the missed call, so cancel the alarm
        	Log.d(TAG, "Cancel missed call alarm");
            AlarmManager mgr=(AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
            Intent i=new Intent(getApplicationContext(), OnAlarmReceiver.class);
            PendingIntent pi=PendingIntent.getBroadcast(getApplicationContext(), 0,
                    i, 0);
            mgr.cancel(pi);
        }
        //update last missed call count
        lastCount = saveCurCount;

        return 0;
	}
	
	class MissedCallsContentObserver extends ContentObserver
	{
	    public MissedCallsContentObserver()
	    {
	        super(null);
	    }

	    @Override
	    public void onChange(boolean selfChange)
	    {
	    	checkCurrentMissedCall();   
	    }
	}	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        MissedCallsContentObserver mcco = new MissedCallsContentObserver();
        getApplicationContext().getContentResolver().registerContentObserver(Calls.CONTENT_URI, true, mcco);
    }
}